package cate.game.role.pvp;

import cate.common.game.GameCollection;
import cate.game.GameBody;
import cate.game.pvp.ladder.RoleLadder;
import cate.game.pvp.ladder.old.LadderOld;
import cate.game.role.Role;
import cate.game.role.RolePart;
import cate.game.role.merge.IRoleMergeSettle;
import cate.game.role.pvp.arena.RoleArena;
import cate.game.role.pvp.championrace.RoleChampionRace;
import cate.game.role.pvp.eliterace.EliteRace;
import cate.game.role.pvp.gradepvp.MyGradePvp;
import cate.game.role.pvp.guildwar.RoleGuildWar;
import cate.game.role.pvp.jungle.MyJunglePO;
import cate.game.role.pvp.minecraft.MyMinecraftPO;
import cate.game.role.pvp.pointrace.PointRace;
import cate.game.role.pvp.pointrace.PointRaceMark;
import cate.game.role.pvp.teampvp.MyTeamPvp;
import cate.game.role.pvp.throne.RoleTB;
import cate.game.role.pvp.videohall.RoleVideoHall;
import cp.solution.util.time.TimeCostRecord;
import easy.java.dev.note.NoteField;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = GameCollection.NAME_ROLE_PVP)
public class RolePvp extends RolePart implements IRoleMergeSettle {
    @NoteField("积分赛")
    public PointRace pointRace;

    @NoteField("积分赛战绩")
    public PointRaceMark pointRaceMark;

    @NoteField(value = "我的录像馆")
    public RoleVideoHall videoHall;

    @NoteField(value = "精英赛")
    public EliteRace eliteRace;

    @NoteField(value = "老天梯")
    public LadderOld ladderOld;

    @NoteField(value = "新天梯赛")
    public RoleLadder ladder;

    @NoteField(value = "矿洞争霸数据")
    public MyMinecraftPO minecraft;

    @NoteField(value = "跨服擂台赛")
    public RoleArena arena;

    @NoteField(value = "公会战")
    public RoleGuildWar guildWar;

    @NoteField("跨服冠军赛")
    public RoleChampionRace championRace;

    @NoteField("丛林冒险")
    public MyJunglePO jungle;

    @NoteField(value = "王座争霸")
    public RoleTB throne;

    @NoteField("全服段位赛")
    public MyGradePvp gradePvp;

    @NoteField("我的组队竞技数据")
    public MyTeamPvp teamPvp;

    public RolePvp() {}

    public RolePvp(Role role) {
        super(role);
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (pointRace == null) {
            pointRace = new PointRace();
        }
        pointRace.initialize(role);
        if (pointRaceMark == null) {
            pointRaceMark = new PointRaceMark();
        }
        pointRaceMark.initialize(role);
        if (videoHall == null) {
            videoHall = new RoleVideoHall();
        }
        videoHall.initialize(role);
        if (eliteRace == null) {
            eliteRace = new EliteRace();
        }
        eliteRace.initialize(role);
        if (minecraft == null) {
            minecraft = new MyMinecraftPO();
        }
        minecraft.initialize(role);
        if (ladderOld == null) {
            ladderOld = new LadderOld();
        }
        ladderOld.initialize(role);
        if (ladder == null) {
            ladder = new RoleLadder();
        }
        ladder.initialize(role);
        if (arena == null) {
            arena = new RoleArena();
        }
        arena.initialize(role);
        if (guildWar == null) {
            guildWar = new RoleGuildWar();
        }
        guildWar.initialize(role);
        if (championRace == null) {
            championRace = new RoleChampionRace();
        }
        championRace.initialize(role);
        if (jungle == null) {
            jungle = new MyJunglePO();
        }
        jungle.initialize(role);
        if (throne == null) {
            throne = new RoleTB();
        }
        throne.initialize(role);
        if (gradePvp == null) {
            gradePvp = new MyGradePvp();
        }
        gradePvp.initialize(role);
        if (teamPvp == null) {
            teamPvp = new MyTeamPvp();
        }
        teamPvp.initialize(role);
        checkOpen();
    }

    @Override
    public void onDaySpan(boolean silence){
        checkOpen();
        pointRace.onDaySpanSafe(silence);
        videoHall.onDaySpanSafe(silence);
        ladder.onDaySpanSafe(silence);
        guildWar.onDaySpanSafe(silence);
        minecraft.onDaySpanSafe(silence);
        jungle.onDaySpanSafe(silence);
        arena.onDaySpanSafe(silence);
        throne.onDaySpanSafe(silence);
        teamPvp.onDaySpanSafe(silence);
        gradePvp.onDaySpanSafe(silence);
    }

    @Override
    public void onWeekSpan(boolean silence) {
        teamPvp.onWeekSpanSafe(silence);
    }

    public void checkOpen(){
        pointRace.checkOpen();
        eliteRace.checkOpen();
        ladder.checkOpen();
        arena.checkOpen();
        minecraft.checkOpen();
        guildWar.checkOpen();
        jungle.checkOpen();
        throne.checkOpen();
        gradePvp.checkOpen();
        teamPvp.checkOpen();
    }

    @Override
    public void onEnter() {
        super.onEnter();
        checkOpen();
        pointRace.onEnter();
        pointRaceMark.onEnter();
        eliteRace.onEnter();
        ladder.onEnter();
        arena.onEnter();
        guildWar.onEnter();
        championRace.onEnter();
        jungle.onEnter();
        throne.onEnter();
        gradePvp.onEnter();
    }

    @Override
    protected void offline() {
        super.offline();
        arena.offlineSafe();
    }

    public void tick() {
        try {
            minecraft.gatherPO.tick();
            arena.tick();
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    @Override
    public void onMergeSettle(GameBody game, TimeCostRecord tcr) {
        // 推荐利用tcr细分记录各操作耗时，以更好追溯潜在问题
    }
}
